<?php
function outLog($a,$b){
    if($b==0){
        echo "[".time()." $a] success\n";
    }else{
        echo "[".time()." $a] failed\n";
        exit($b);
    }
}
function getOS(): string
{
    $a=php_uname("s");
    for ($i=65;$i<=90;$i++){
        if (is_dir(chr($i).":\\Windows")){
            return "Windows";
        }
    }
    return"Linux";
}
function run($a,$b){
    exec("$a $b",$c,$d);
    if ($d==1){
        echo $b."\n";
        echo json_encode($c)."\n";
    }
    outLog($a,$d);
}
function readFunction($path){
    $a=@file_get_contents($path);
    $r=[];
    if ($a){
        if (!readString($a,"<?php")){
            echo "Not a normal php file!";
        }
        if (readString($a,"loadErrorPage")){
            return $r;
        }
        if (!readString($a,"class get")&&!readString($a,"class post")){
            echo "Format error!(class)\n";
            echo $path;
            return $r;
        }
        if (!readString($a,"{")){
            echo "Format error!( { )";
            return $r;
        }

//        echo $a;
        while(true){
            if (!readString($a,"public")){
                break;
            }
            if (!readString($a,"static")){
                break;
            }
            if (!readString($a,"function")){
                break;
            }
//            echo($a);
            $index=0;
            $name="";
            while(true){
                $x=substr($a,$index,1);
                if($x=="("){
                    break;
                }else{
                    $name.=$x;
                    $index++;
                }
            }
            if (!readString($a,"$name():bool")){
                break;
            }
            $count=0;
            $code_content="";
            $index=0;
            while(true){
                $x=substr($a,$index++,1);
                if ($x=="{"){
                    $count++;
                }elseif($x=="}"){
                    $count--;
                }
                $code_content.=$x;
                if ($count==0){
                    $r[]=[$name,$code_content];
//                    goto b;
                    break;
                }
            }
            readString($a,$code_content);
            if (readString($a,"}")){
                break;
            }
        }
        return $r;
    }else{
        echo "[".time()." readFunction] warning\n";
        echo $path."\n";
        return $r;
    }
//    return $r;
}
function readString(&$string,$content):bool{
    if (substr($string,0,strlen($content))==$content){
        $string=ltrim(substr($string,strlen($content)));
        return true;
    }else{
        return false;
    }
}
if (getOS()=="Windows"){
    $start_time=time();
    if (!is_dir("src")){
        outLog("load",1);
    }
    if(is_dir("split")){
        run("rd","/s /q split");
    }
    run("md","split");
    $a=scandir("src/core");
    foreach ($a as $item) {
        if ($item!="."&&$item!=".."&&is_dir("src/core/$item")&&$item!="mail"){
            run("md","split\\$item");
            run("md","split\\$item\\static");
            run("md","split\\$item\\lib");
            run("copy","src\\core\\public.php split\\$item\\lib\\");
            touch("split\\$item\\lib\\define.php");
            echo "[".time()." fixConfig] start\n";
            run("md","split\\$item\\config");
            run("md","split\\$item\\common");
            run("md","split\\$item\\common\\html");
            run("xcopy","src\\static\\common\\html\\* split\\$item\\common\\html\\ /E /C /I");
            touch("split\\$item\\config\\global.prop");
            file_put_contents("split\\$item\\config\\global.prop",<<<EOF
timezone=PRC
enableStatic=1
enableDomains=1
enableSSL=0
EOF
);
            run("copy","config\\domains.json split\\$item\\config\\");
            echo "[".time()." fixDefine] start\n";
            file_put_contents("split\\$item\\lib\\define.php",<<<EOF
<?php
define("PATH", \$_SERVER["DOCUMENT_ROOT"]."/");
const STATIC_PATH = PATH . "static/";
const HTML_PATH = STATIC_PATH . "html/";
const JS_PATH = STATIC_PATH . "js/";
const CSS_PATH = STATIC_PATH . "css/";
const CONFIG_PATH = PATH . "config/";
const GLOBAL_LOG_PATH = PATH . "log/";
const FILES_PATH = STATIC_PATH . "files/";
const LOG_PATH = PATH . "log/";
const COMMON_PATH = PATH . "common/";
const COMMON_HTML_PATH = COMMON_PATH . "html/";
const RETURN_FORMAT_STRING = 1;
const RETURN_FORMAT_JSON = 2;
const RETURN_FORMAT_array = 3;
EOF
);
            echo "[".time()." buildPHP] start\n";
            $b=readFunction("src\\core\\$item\\get.php");
            echo "[".time()." readGet] success\n";
//            echo json_encode($b);
            run("md","split\\$item\\file\\");
            touch("split\\$item\\file\\index.php");
            file_put_contents("split\\$item\\file\\index.php",<<<EOF
<?php
include(\$_SERVER["DOCUMENT_ROOT"]."\\lib\\define.php");
include(\$_SERVER["DOCUMENT_ROOT"]."\\lib\\public.php");
\$a = \$_SERVER["REQUEST_METHOD"];
if (\$a=="GET"){
    include("get.php");
    get::file();
}elseif(\$a=="POST"){
    include("post.php");
    post::file();
}
EOF
);
            touch("split\\$item\\file\\get.php");
            file_put_contents("split\\$item\\file\\get.php",<<<EOF
<?php
class get{
    public static function file(){
        \$type=args_get("type");
        \$name=args_get("name");
        switch(\$type){
            case "js":
                header('Content-Type:applcation/javascript');
                echo file_get_contents(JS_PATH.\$name.".js");
                break;
            case "css":
                header('Content-Type:text/css;charset=utf-8');
                echo file_get_contents(CSS_PATH.\$name.".css");
                break;
            case "jpg":
            case "jpeg":
                header('Content-Type:image/jpeg;charset=utf-8');
                echo file_get_contents(IMG_PATH.\$name.".jpg");
                break;
            case "svg":
                header('Content-Type:text/xml;charset=utf-8');
                echo file_get_contents(IMG_PATH.\$name.".svg");
                break;
            case "woff2":
            case "woff":
            case "ttf":
            case "eot":
                header('Content-Type:application/octet-stream;charset=utf-8');
                writeLog("Info","Read file ".FILES_PATH.\$name);
                echo file_get_contents(FILES_PATH.\$name);
                break;
            default:
                echo "文件格式暂不支持";
        }
    }
}
EOF
);

            foreach ($b as $itemB) {
                $name=$itemB[0];
                $name=str_replace("_","\\",$name);
                $code_content=$itemB[1];
                if (!is_dir("split\\$item\\$name")) {
                    run("md", "split\\$item\\$name");
                }
                touch("split\\$item\\$name\\index.php");
                echo "[".time()." makeIndex] success\n";
                file_put_contents("split\\$item\\$name\\index.php",<<<EOF
<?php
include(\$_SERVER["DOCUMENT_ROOT"]."\\lib\\define.php");
include(\$_SERVER["DOCUMENT_ROOT"]."\\lib\\public.php");
\$a = \$_SERVER["REQUEST_METHOD"];
if (\$a=="GET"){
    include("get.php");
    get::$name();
}elseif(\$a=="POST"){
    include("post.php");
    post::$name();
}
EOF);
                touch("split\\$item\\$name\\get.php");
                echo "[".time()." makeGet] success\n";
                file_put_contents("split\\$item\\$name\\get.php",<<<EOF
<?php
class get{
    public static function $name()$code_content
}
EOF
);
            }

            $b=readFunction("src\\core\\$item\\post.php");
            foreach ($b as $itemB) {
                $name=$itemB[0];
                $name=str_replace("_","\\",$name);
                $code_content=$itemB[1];
                if (!is_dir("split\\$item\\$name")) {
                    run("md", "split\\$item\\$name");
                }
                touch("split\\$item\\$name\\index.php");
                echo "[".time()." makeIndex] success\n";
                file_put_contents("split\\$item\\$name\\index.php",<<<EOF
<?php
include(\$_SERVER["DOCUMENT_ROOT"]."\\lib\\define.php");
include(\$_SERVER["DOCUMENT_ROOT"]."\\lib\\public.php");
\$a = \$_SERVER["REQUEST_METHOD"];
if (\$a=="GET"){
    include("get.php");
    get::$name();
}elseif(\$a=="POST"){
    include("post.php");
    post::$name();
}
EOF);
                touch("split\\$item\\$name\\post.php");
                echo "[".time()." makePost] success\n";
                file_put_contents("split\\$item\\$name\\post.php",<<<EOF
<?php
class post{
    public static function $name()$code_content
}
EOF
);
            }

        }
    }
    echo "[".time()." makePHP] success\n";
    echo "[".time()." makeStatic] start\n";
    foreach ($a as $item) {
        if ($item != "." && $item != ".." && is_dir("src/core/$item") && $item != "mail") {
            if (is_dir("src\\static\\$item")) {
                run("xcopy", "src\\static\\$item\\* split\\$item\\static\\ /E /C /I");
            }
        }
    }
    echo "[".time()." makeStatic] success\n";
    echo "[".time()." fixIndexPage] start\n";
    foreach ($a as $item) {
        if ($item != "." && $item != ".." && is_dir("src/core/$item") && $item != "mail") {
            if (is_dir("split\\$item\\index")) {
                run("copy","split\\$item\\index\\*.php split\\$item\\");
            }
        }
    }
    echo "[".time()." fixIndexPage] success\n";
    echo "finished in ".(time()-$start_time)."s.";
}else{
    echo "[".time()." failed] you can only run this script on windows.";
}